package com.gopay.bis.web.controller.ebank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.PayEaseConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.payease.ebank.PayEaseMessage;

/**
 * 首信通道前台通知
 * 
 * @UmPayFrontRes.java
 * @author liuzhp
 * @2013-12-4 下午4:11:00 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class PayEasePayFrontRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(PayEasePayFrontRes.class);
	
	@RequestMapping(value = "/payEasePayFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- PayEasePayFrontRes bankback  ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		PayEaseMessage msg = new PayEaseMessage();
		msg.setPlainMessage(request.getParameter("v_oid"));
		msg.setVpstatus(request.getParameter("v_pstatus"));
		msg.setVpstring(request.getParameter("v_pstring"));
		msg.setVpmode(request.getParameter("v_pmode"));
		msg.setOrderId(request.getParameter("v_oid").split("-")[2]);
		msg.setBankOrderId(request.getParameter("v_oid"));
		msg.setVamount(request.getParameter("v_amount"));
//		msg.setSignMessage(request.getParameter("v_md5money"));
		msg.setVmoneytype(request.getParameter("v_moneytype"));
		msg.setVmd5info(request.getParameter("v_md5info"));
		msg.setFront(true);				
		msg.setBankBusProd(BisBankBusinessProd._00);
		msg.setVsign(request.getParameter("v_sign"));
		msg.setPlainMessage(msg.getPlainMessage() + msg.getVpstatus() + msg.getVamount() + msg.getVmoneytype());
		return msg;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		logger.error("PAYEASE FRONT:"+ ToStringBuilder.reflectionToString((PayEaseMessage) result.getModel("message")));
		OrderInfo orderInfo = new OrderInfo();
		PayEaseMessage easeMessage = (PayEaseMessage) result.getModel("message");
		if(easeMessage!=null){
			String trade_state = request.getParameter("v_pstatus");
			if (trade_state != null&& trade_state.equals(PayEaseConstants.TRAN_STATE_SUCCESS)) {
				orderInfo.setOrderId(easeMessage.getOrderId());
				orderInfo.setAmt(easeMessage.getVamount());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("支付成功");
			} else {
				orderInfo.setOrderId(easeMessage.getOrderId());
				orderInfo.setAmt(easeMessage.getVamount());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg("");
			}
		}
		orderInfo.setCardType("Unknown");
		orderInfo.setIsNotify(false);
		orderInfo.setBankName("payease");
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("v_oid"))
				|| StringUtils.isEmpty(request.getParameter("v_pstatus"))
				|| StringUtils.isEmpty(request.getParameter("v_amount"))) {
		return false;
		}
		return true;
	}
}
